<?php

namespace app\controller;
use think\facade\Db;
use think\Request;

class Xhsanalysis extends Common
{
	public function read(Request $request)
	{
		if($this->hasPermission($request->sessInfo, 'xhsanalysis:read')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}

		$where = [];
		$limit = $request->param('limit/d', 20);
		$order = $request->param('order', 'desc', 'trim');
		$sort = $request->param('sort', 'id', 'trim');

		$bazaarId = $request->param('bazaarId', '', 'trim');
		if (empty($bazaarId) == false) {
			array_push($where, ['bazaarId', '=', $bazaarId]);
		}

		$keywords = $request->param('keywords', '', 'trim');
		if (empty($keywords) == false) {
			array_push($where, ['keywords', 'like', "%{$keywords}%"]);
		}

		$createTimeStart = $request->param('createTimeStart', '', 'trim');
		$createTimeEnd = $request->param('createTimeEnd', '', 'trim');
		if( $createTimeStart && $createTimeEnd ) {
			$time = cloud_datetime_format( $createTimeStart, $createTimeEnd, true);
		} else {
			$time = cloud_datetime_format( date('Y-m-d 00:00:00'), date('Y-m-d 00:00:00', strtotime("+1 day")), true);
		}
		array_push($where, ['datetime', 'BETWEEN', $time]);

		$paginate = Db::name('cfg_xhs_keywords_count')->field(
				'`id`, `bazaarId`, `keywords`, `advertisingNumber`, `sponsorNumber`, `urlNumber`, `countNumber`'
		)->where($where)->order($sort, $order)->paginate($limit);

		$total = Db::name('cfg_xhs_keywords_count')->field(
				' sum(`advertisingNumber`) as `advertisingNumber`, sum(`sponsorNumber`) as `sponsorNumber`, sum(`urlNumber`) as `urlNumber`, sum(`countNumber`) as `countNumber`'
		)->where($where)->find();

		$total['advertisingNumber'] = $total['advertisingNumber'] ? $total['advertisingNumber'] : 0;
		$total['sponsorNumber'] = $total['sponsorNumber'] ? $total['sponsorNumber'] : 0;
		$total['urlNumber'] = $total['urlNumber'] ? $total['urlNumber'] : 0;
		$total['countNumber'] = $total['countNumber'] ? $total['countNumber'] : 0;
		$xhsBazaar = Db::table('cfg_xhs_bazaar')->field('id, name')->where('status', 1)->select()->toArray();

		return json(['code' => 0, 'msg' => '', 'count' => $paginate->total(), 'data' => $paginate->items(), 'total' => $total, 'bazaar' => $xhsBazaar]);
	}


	public function getData(Request $request)
	{
		if($this->hasPermission($request->sessInfo, 'xhsanalysis:read')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}

		$where = [];

		$keywords = $request->param('keywords', '', 'trim');
		if (empty($keywords) == false) {
			array_push($where, ['keywords', 'like', "%{$keywords}%"]);
		}

		$createTimeStart = $request->param('createTimeStart', '', 'trim');
		$createTimeEnd = $request->param('createTimeEnd', '', 'trim');
		if( $createTimeStart && $createTimeEnd ) {
			array_push( $where, ['lasttime', 'between', cloud_datetime_format( $createTimeStart, $createTimeEnd, true) ] );
		} else {
			array_push( $where, ['lasttime', 'between', cloud_datetime_format( date('Y-m-d 00:00:00'), date('Y-m-d 00:00:00', strtotime("+1 day")), true) ] );
		}

		$paginate = Db::name('cfg_xhs_url')->field(
				'`id`, `bazaarId`, `type`, `title`, `company`, `locationTV`, `comments`, `datetime`, `showpos`, `love`, `star`, `url`, `urlex`, `keywords`, `number`,
				FROM_UNIXTIME(`time`, \'%Y-%m-%d %H:%i:%s\') as `time`, FROM_UNIXTIME(lasttime, \'%Y-%m-%d %H:%i:%s\') as lasttime'
		)->where($where)->order('lasttime', 'desc')->select()->toArray();

		return json(['code' => 0, 'msg' => '', 'data' => $paginate ]);
	}
}
